package org.example.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.pojo.SysUser;
import org.example.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/user/add")
    public boolean addUser(@RequestBody SysUser sysUser){
        return sysUserService.addSysUser(sysUser);
    }

    @GetMapping("/user/get/{id}")
    public SysUser getUser(@PathVariable Integer id){
        SysUser sysUser = sysUserService.queryById(id);
        return sysUser;
    }

    @GetMapping("/user/list")
    public List<SysUser> queryAll(){
        List<SysUser> sysUsers = sysUserService.queryAll();
        return sysUsers;
    }

    //服务发现   获取注册的服务信息
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/user/discovery")
    public Object discovery(){
        //获取注册服务
        List<String> services = discoveryClient.getServices();

        for (String service : services) {
            System.out.println("service = " + service);
        }
        //根据服务id获取服务列表
        List<ServiceInstance> list = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-USER");
        for (ServiceInstance instance : list) {
            System.out.println(
                    instance.getHost() + "\t" +
                            instance.getPort() + "\t" +
                            instance.getUri() + "\t" +
                            instance.getServiceId()
            );
        }
        return list;
    }



}
